package com.mycompany.System.controller;

import com.mycompany.System.service.SystemService;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.mvc.weixinUtils.SignUtil;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by Jintao_Ma on 2017/9/4.
 */
@Controller
@RequestMapping("systemController")
public class SystemController{

    @Autowired
    private SystemService systemService;

    /**接受微信发来的认证消息 get请求
     * 2017年9月4日22:22:39
     * @Author Jintao_Ma
     */
    @RequestMapping(value = "weixin",method = RequestMethod.GET)
    @ResponseBody
    public String weixinGet(HttpServletRequest request){
        Map<String,Object> weixinMap = WebUtils.getParametersStartingWith(request, "");
        return  SignUtil.checkSignature(weixinMap)?
                MapUtils.getString(weixinMap,"echostr", SystemUtil.Empty):SystemUtil.Empty;
    }

    /**接受微信发来的认证消息 post请求
     * 2017年9月4日22:22:39
     * @Author Jintao_Ma
     */
    @RequestMapping(value = "weixin",method = RequestMethod.POST)
    public void weixinPost(HttpServletRequest request, HttpServletResponse response){
        systemService.processRequest(request,response);
    }

}
